﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Admin_quan_ly_slide_tin_tuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetAllNewsSlide();
            LoadPost();
        }
    }
    protected void grvNewsSlide_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvNewsSlide.EditIndex = -1;
        GetAllNewsSlide();
    }

    protected void grvNewsSlide_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string i = Convert.ToString(grvNewsSlide.DataKeys[e.RowIndex].Value);
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_DeleteSlide_TinTuc";
            Dal.ParameterCollection = new string[1] { "@News_SlideID" };
            Dal.ValueCollection = new string[1] {i};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAllNewsSlide();
        }
        catch { }
    }
    protected void grvNewsSlide_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvNewsSlide.EditIndex = e.NewEditIndex;
        GetAllNewsSlide();
    }
    protected void grvNewsSlide_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txttitle = (TextBox)grvNewsSlide.Rows[e.RowIndex].FindControl("txtTitle");
        TextBox txtDescrip = (TextBox)grvNewsSlide.Rows[e.RowIndex].FindControl("txtDescrip");
        FileUpload fuBanner = (FileUpload)grvNewsSlide.Rows[e.RowIndex].FindControl("fuBanner");
        CheckBox cbactive = (CheckBox)grvNewsSlide.Rows[e.RowIndex].FindControl("cbactive1");
        string i = Convert.ToString(grvNewsSlide.DataKeys[e.RowIndex].Value);
        if (fuBanner.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(fuBanner.FileName);
                fuBanner.SaveAs(Server.MapPath("Images/NewsSlide/" + filename));
                SQLHelper Dal = new SQLHelper();
                Dal.Open();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_UpdateSlide_TinTucWithImage";
                Dal.ParameterCollection = new string[5] { "@TieuDe", "@MoTa", "@News_SlideID", "@DuongDan", "@IsActive" };
                Dal.ValueCollection = new string[5] { txttitle.Text, txtDescrip.Text, i, "Images/NewsSlide/" + fuBanner.FileName, cbactive.Checked.ToString() };
                Dal.ExecuteNonQuery();
                Dal.Close();
                Dal.Dispose();
            }
            catch { }
        }
        else
        {
            try {
                SQLHelper Dal = new SQLHelper();
                Dal.Open();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_UpdateSlide_TinTucWithNoneImage";
                Dal.ParameterCollection = new string[4] { "@TieuDe", "@MoTa", "@News_SlideID", "@IsActive" };
                Dal.ValueCollection = new string[4] { txttitle.Text, txtDescrip.Text, i,cbactive.Checked.ToString() };
                Dal.ExecuteNonQuery();
                Dal.Close();
                Dal.Dispose();
            }
            catch { }
        }
    }
    protected void GetAllNewsSlide()
    {
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllSlideNews";
            DataTable db = Dal.GetDataTable();
            grvNewsSlide.DataSource = db;
            grvNewsSlide.DataBind();
        }
        catch { }
    }
    protected void LoadPost()
    {
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetNews";
            DataTable db = Dal.GetDataTable();
            ddlPost.DataSource = db;
            ddlPost.DataTextField = db.Columns["TieuDe"].ToString();
            ddlPost.DataValueField = db.Columns["BaiVietID"].ToString();
            ddlPost.DataBind();
        }
        catch { }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        try 
        {
            string filename = Path.GetFileName(fuBanner.FileName);
            fuBanner.SaveAs(Server.MapPath("~/Images/NewsSlide/" + filename));
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_AddSlide_TinTuc";
            Dal.ParameterCollection = new string[5] { "@TieuDe", "@MoTa", "@BaiVietID", "@DuongDan","@IsActive"};
            Dal.ValueCollection = new string[5] { txtTitle.Text, txtDescrip.Text, ddlPost.SelectedValue, "Images/NewsSlide/" +filename, rblActive.SelectedValue };
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();

        }
        catch { }
    }
}
